package com.hnluchuan.bmjlearning.service;

import java.util.ArrayList;
import java.util.List;

import com.hnluchuan.bmjlearning.common.ResultCode;
import com.hnluchuan.bmjlearning.dao.UserDAO;
import com.hnluchuan.utils.exception.BizException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hnluchuan.bmjlearning.dao.AgentDAO;
import com.hnluchuan.bmjlearning.model.Agent;
import com.hnluchuan.bmjlearning.dto.AgentDTO;
import com.hnluchuan.core.support.Page;
import com.hnluchuan.utils.common.BeanMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AgentService extends BaseService {
	
	@Autowired
	private AgentDAO agentDAO;
	@Autowired
	private UserDAO userDAO;
	
	public List<AgentDTO> find(AgentDTO query, Page page) {
		List<Agent> models = agentDAO.find(query, page);
		List<AgentDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<AgentDTO> findAll() {
		return this.find(new AgentDTO(), null);
	}
	
	public List<AgentDTO> findAll(AgentDTO query) {
		return this.find(query, null);
	}
	
	public List<AgentDTO> find(Page page) {
		return this.find(new AgentDTO(), page);
	}
	
	public List<AgentDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new AgentDTO(), page);
	}

	public AgentDTO findByUserId(Long userId){
		return toDTO(agentDAO.findByUserId(userId));
	}
	
	public Long create(AgentDTO dto) {
		Agent model = new Agent();
		toModel(model, dto);
		return agentDAO.create(model);
	}
	
	public AgentDTO load(Long id) {
	    Agent model = agentDAO.load(id);
	    return toDTO(model);
    }

	public void updateAllFields(AgentDTO dto) {
		Agent model = agentDAO.load(dto.getId());
		toModel(model, dto);
		agentDAO.update(model);
    }

    @Transactional(rollbackFor = Exception.class)
	public void deleteOrActivateById(Long userId, Long agentId, Integer status) {
	    int updatedAgentCount = agentDAO.deleteOrActivateAgentByAgentId(agentId, status);
	    int updatedUserCount = userDAO.deleteOrActivate(userId, status);
	    if(updatedAgentCount <= 0 || updatedUserCount <= 0){
	    	throw new BizException(ResultCode.OperationFailed.getRemark());
		}
    }

	public AgentDTO findOne(AgentDTO query) {
		Agent model = agentDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<AgentDTO> toDTOs(List<Agent> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<AgentDTO>(0);
		}
		List<AgentDTO> dtos = new ArrayList<AgentDTO>(models.size());
		for (Agent model : models) {
	        AgentDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private AgentDTO toDTO(Agent model) {
		if (model == null) {
			return null;
		}
		AgentDTO dto = BeanMapper.map(model, AgentDTO.class);
		
		return dto;
	}
	
	private void toModel(Agent model, AgentDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<Agent> toModels(List<AgentDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<Agent>(0);
		}
		List<Agent> models = new ArrayList<Agent>(dtos.size());
		for (AgentDTO dto : dtos) {
	        Agent model = new Agent();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
